home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / netscape / ntscp201.486 / ntscp201 / hot-convert.sh next >
Linux/UNIX/POSIX Shell Script  |  1995-03-14  |  3KB  |  151 lines

  1. #!/bin/sh
  2. # Script for converting NCSA xmosaic hotlist files to Netscape's format.
  3. # Copyright © 1995 Netscape Communications Corp., all rights reserved.
  4. # Created: Jamie Zawinski <jwz@netscape.com>, 17-Oct-94.
  5. # Bugs and commentary to x_cbug@netscape.com.
  6.  
  7. usage()
  8. {
  9.   echo "usage: $0 [ input-file [ output-file ]]" >&2
  10.   exit 1
  11. }
  12.  
  13. #set -x
  14. set -e
  15.  
  16. input=__DEFAULT__
  17. output=$HOME/.netscape-bookmarks.html
  18. tmp=/tmp/hc$$
  19. tmp2=/tmp/hc$$2
  20.  
  21. create_p=false
  22.  
  23. if [ $# = 2 ]; then
  24.   input="$1"
  25.   output="$2"
  26. elif [ $# = 1 ]; then
  27.   input="$1"
  28. elif [ $# != 0 ]; then
  29.   usage
  30. fi
  31.  
  32. if [ "$input" = __DEFAULT__ ]; then
  33.   if [ -f $HOME/.mosaic-hotlist-default.html ]; then
  34.     input=$HOME/.mosaic-hotlist-default.html
  35.   else
  36.     input=$HOME/.mosaic-hotlist-default
  37.   fi
  38. fi
  39.  
  40. if [ ! -f $input ]; then
  41.   echo $0: input file $input does not exist.
  42.   usage
  43. fi
  44.  
  45. if [ ! -f $output ]; then
  46.   create_p=true
  47. fi
  48.  
  49. rm -f $tmp $tmp2
  50.  
  51. firstline="`head -1 $input`"
  52.  
  53. format=0;
  54. if [ "$firstline" = "ncsa-xmosaic-hotlist-format-1" ]; then
  55.   format=1
  56. elif [ "$firstline" = "<HTML>" ]; then
  57.   secondline="`head -2 $input | tail -1 | sed 's/>.*/>/'`"
  58.   if [ "$secondline" = "<!-- ncsa-xmosaic-hotlist-format-2 -->" ]; then
  59.     format=2
  60.   elif [ "$secondline" = "<TITLE>" ]; then
  61.     thirdline="`head -3 $input | tail -1`"
  62.     if [ "$thirdline" = "Default" ]; then
  63.       format=2
  64.     fi
  65.   fi
  66. fi
  67.  
  68. if [ "$format" = 0 ]; then
  69.   echo "$0: input file $input is not an NCSA xmosaic hotlist file"
  70.   usage
  71. fi
  72.  
  73. #if [ $create_p != true ]; then
  74. #  if [ "`head -1 $output`" != "<!DOCTYPE MCOM-Bookmark-file-1>" ]; then
  75. #   if [ "`head -1 $output`" != "<!DOCTYPE NETSCAPE-Bookmark-file-1>" ]; then
  76. #    echo "$0: output file $output exists but is not a Netscape bookmarks file"
  77. #    usage
  78. #   fi
  79. #  fi
  80. #fi
  81.  
  82. size="`wc -l $input | awk '{print $1}'`"
  83.  
  84. if [ "$size" -le 2 ]; then
  85.   echo "$0: input file $input contains no hotlist items"
  86.   usage
  87. fi
  88.  
  89. # Take off the first two lines of the input
  90. if [ $format = 1 ]; then
  91.  
  92.   tail +3 $input |
  93.  
  94.   awk 'BEGIN { state=0; }
  95.   {
  96.     if ( state == 0 )
  97.       {
  98.         printf ("    <DT><A HREF=\"%s\">", $1);
  99.         state = 1;
  100.       }
  101.     else
  102.       {
  103.         printf ("%s</A>\n", $0);
  104.         state = 0;
  105.       }
  106.   }
  107.   ' > $tmp
  108.  
  109. else
  110.  
  111.   # new format
  112.   tail +5 $input | sed '
  113.     s@^<LI> *@    <DT>@;
  114.     s@^</UL> *$@@;
  115.     s@^</HTML> *$@@;
  116.    ' > $tmp
  117.  
  118. fi
  119.  
  120.  
  121. if [ $create_p = true ]; then
  122.  
  123.   echo "<!DOCTYPE NETSCAPE-Bookmark-file-1>"          >> $tmp2
  124.   echo "<!-- This is an automatically generated file.>"   >> $tmp2
  125.   echo "     It will be read and overwritten."            >> $tmp2
  126.   echo "     Do Not Edit! -->"                            >> $tmp2
  127.   echo "<TITLE>$USER's Bookmarks</TITLE>"                 >> $tmp2
  128.   echo "<H1>$USER's Bookmarks</H1>"                       >> $tmp2
  129.   echo "<DL><p>"                                          >> $tmp2
  130.   cat $tmp >> $tmp2
  131.   echo "</DL><p>"                                         >> $tmp2
  132.   cat $tmp2 > $output
  133.  
  134.   echo "$0: created bookmarks file $output" >&2
  135.  
  136. else
  137.  
  138.   # take off the last closing /DL (the flushleft one) so that we can put
  139.   # the new items before it.
  140.   sed 's@^</DL><p>@@' < $output > $tmp2
  141.   echo "<HR>" >> $tmp2
  142.   cat $tmp >> $tmp2
  143.   echo "</DL><p>" >> $tmp2
  144.   cat $tmp2 > $output
  145.  
  146.   echo "$0: appended to bookmarks file $output" >&2
  147.  
  148. fi
  149.  
  150. rm -f $tmp $tmp2
  151.